#include "dofs.h"


#define SESS_MAX    64

typedef struct {
    char codec[8];

    struct {
        char width[8];
        char height[8];
        char format[8];
    } port[2];

    char name[64];
} sess_t;

#define CLEAN(x) memset(x, 0, sizeof(x))

#define SESS_PATH   "/sys/kernel/debug/amvx_if0/session"

char* find_last_char(char* start, char* end, char ch) {
    char* last_char = NULL; // 初始化最后一个空格的指针为空

    while (start <= end) {
        if (*start == ch) {
            last_char = start; // 更新最后一个空格的指针
        }
        start++;
    }

    return last_char;
}

char *get_table_value(char *table, char *key, char *value)
{
    char *start, *end;

    start = strstr(table, key);
    if (start == NULL) {
        return NULL;
    }

    end = strchr(start, '\n');
    if (end == NULL) {
        return NULL;
    }

    start = find_last_char(start, end, ' ');
    if (start == NULL) {
        return NULL;
    }

    //printf("start=%p, end=%p, n=%d\n", start, end, end - start - 1);
    strncpy(value, start + 1, end - start - 1);

    return (end + 1);
}

char hex2ch(char hex)
{
    if (hex >= '0' && hex <= '9')
        return hex - '0';

    if (hex >= 'a' && hex <= 'f')
        return hex - 'a' + 10;

    if (hex >= 'A' && hex <= 'F')
        return hex - 'A' + 10;
}


void get_port(sess_t *sess)
{
    char buf[1024] = {0};
    char path[1024] = {0};

    for (int i = 0; i < 2; i++) {
        char fourcc[16] = {0};
        CLEAN(buf);
        CLEAN(path);

        sprintf(path, "%s/%s/port%d/stat", SESS_PATH, sess->name, i);

        if (get_str(path, buf, sizeof(buf)) < 0) {
            return;
        }

        get_table_value(buf, "width", sess->port[i].width);
        // printf("width=%s\n", sess->port[i].width);
        get_table_value(buf, "height", sess->port[i].height);

        get_table_value(buf, "pixelformat", fourcc);
        if (strlen(fourcc) != 10) {
            return;
        }
        sess->port[i].format[0] = hex2ch(fourcc[8]) << 4 | hex2ch(fourcc[9]);
        sess->port[i].format[1] = hex2ch(fourcc[6]) << 4 | hex2ch(fourcc[7]);
        sess->port[i].format[2] = hex2ch(fourcc[4]) << 4 | hex2ch(fourcc[5]);
        sess->port[i].format[3] = hex2ch(fourcc[2]) << 4 | hex2ch(fourcc[3]);
    }
}

int main(int argc, char *argv[])
{
    sess_t sessions[SESS_MAX] = {0};

    memset(sessions, 0, sizeof(sessions[SESS_MAX]));

    int count = 0;

    DIR *dir;
    struct dirent *dirent;

    // 打开目录
    dir = opendir(SESS_PATH);
    if (dir == NULL) {
        fatal("can't open dir: %s", SESS_PATH);
    }

    // 遍历目录
    while ((dirent = readdir(dir)) != NULL) {
        if (dirent->d_name[0] == '.')
            continue;
        strcpy(sessions[count].name, dirent->d_name);
        get_port(&sessions[count]);
        count++;
    }

    // 关闭目录
    closedir(dir);
    for (int i=0; i < count; i++) {
        sess_t *s = &sessions[i];
        printf("%-2d | %s | %s %s %s | %s %s %s\n", \
                i + 1, \
                s->name, \
                s->port[0].width, s->port[0].height, s->port[0].format, \
                s->port[1].width, s->port[1].height, s->port[1].format);

    }
}
